﻿
$(document).ready(function () {

    //Gan su kien click cho button create product
    BindingEvent_buttonCreateProduct();

    //Gan su kien click cho button delete product
    BindingEvent_buttonEditProduct();

    //Gan su kien click cho button delete product
    BindingEvent_buttonDeleteProduct();

    //Gan su kien click cho tieu de ProductName
    BindingEvent_linkProductSortByName();

});

function BindingEvent_buttonCreateProduct() {

    $('.btnCreate').off('click');
    $('.btnCreate').on('click', function () {

        ProgressShow();
        $(document.body).addClass('modal-noscrollbar');
        CustomLayoutModal();

        var url = baseUrl + "Create"; // the url to the controller
        $.get(url, function (data) {
            ProgressHide();

            $('#edit-product-container').html(data);
            $('#edit-product').modal('show');
            $(document.body).addClass('modal-noscrollbar');
            CustomLayoutModal();
        });
    });
}

function BindingEvent_buttonEditProduct() {

    $('.ProductUpdate_btnEdit').off('click');
    $('.ProductUpdate_btnEdit').on('click', function () {

        ProgressShow();
        $(document.body).addClass('modal-noscrollbar');
        CustomLayoutModal();
        
        var url = baseUrl + "Edit"; // the url to the controller
        var id = $(this).attr('data-id'); // the id that's given to each button in the list
        $.get(url + '/' + id, { id: id }, function (data) {
            ProgressHide();

            $('#edit-product-container').html(data);
            $('#edit-product').modal('show');
            $(document.body).addClass('modal-noscrollbar');
            CustomLayoutModal();
        });
    });
}

function BindingEvent_buttonDeleteProduct() {

    $('.btn.btn-sm.btnDelete').off('click');
    $('.btn.btn-sm.btnDelete').on('click', function () {
        
        var form = $('#FunctionDeleteProduct');
        var token = $('input[name="__RequestVerificationToken"]', form).val();
        
        var productid = $(this).attr('data-id');
        var url = baseUrl + "Delete";
        var msgConfirm = confirm('Are you sure you wanna delete this product?');

        if (msgConfirm==true)  {
            $.ajax({
                url: url,
                data: {
                    __RequestVerificationToken: token,
                    id: productid
                },
                type: 'POST',
                success: function (data) {
                    $('#table table-responsive').html(data);
                },
                error: function (request, status, error) {
                    alert(request.responseText);
                }
            });
        }
    });
}

function BindingEvent_linkProductSortByName() {

    $('#lnkHeadProductName').off('click');
    $('#lnkHeadProductName').on('click', function () {
        var xSortOrder = '';
        var url = baseUrl + "Index";

        $.ajax({
            url: url,
            data: { sortOrder: xSortOrder },
            type: 'GET',
            dataType: "html",
            success: function (data) {
                //$('.container.body-content').html(data);
                //$('.container.body-content').load(data);
                //window.location.href = url;
                window.location = url;
            },
            error: function (request, status, error) {
                alert(request.responseText);
            },
            //complete: function () {
            //    var window = $("#SupplierQuoteDialog").data("tWindow");
            //    window.center().open();
            //}

        });
    });
}